package core;

import org.kxml2.kdom.Element;
import org.kxml2.kdom.Node;

public class WeekDefinition{

	private int m_weekNumber;
	private static final String m_weeks[] = {"Upper", "Lower", "Both"};
	
	public WeekDefinition(int weekNumber){
		setWeek(weekNumber);
	}
	
	public void setWeek(int weekNumber){
		if(weekNumber >= m_weeks.length)
			m_weekNumber = m_weeks.length - 1;
		else if(weekNumber < 0)
			m_weekNumber = 0;
		else
			m_weekNumber = weekNumber;
	}
	
	public String getWeekName(){
		return m_weeks[m_weekNumber];
	}
	
	public int getWeekNumber(){
		return m_weekNumber;
	}
	
	public boolean equals(Object other) {
		WeekDefinition otherWeek = (WeekDefinition)other;
		return m_weekNumber == otherWeek.m_weekNumber;
		
	}
	
	public static String[] getWeeeksNames(){
		return m_weeks;
	}
	
	public void toXml(Node node){
		Element wdNode = node.createElement(null, "WeekDefinition");
		wdNode.setAttribute(null, "number", new Integer(m_weekNumber).toString());
		node.addChild(Node.ELEMENT, wdNode);        
	}
	
	public static WeekDefinition fromXml(Element elem) throws Throwable{
		try
	    {
			Element rootElem = elem; 
			if (!rootElem.getName().equals("WeekDefinition")){
				rootElem = elem.getElement(null, "WeekDefinition");
			}
			
			int num = Integer.parseInt(rootElem.getAttributeValue(null, "number"));
			return new WeekDefinition(num);
	    }
		catch (Exception e) {
			throw e;
		}
	}
}
